package blog;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitEnd{

    /**
     * 注册
     * @param userName
     * @param password
     * @throws InterruptedException
     */
    @Disabled
    @Order(1)
    @ParameterizedTest
    @MethodSource("dataRegister")
    public void Register(String userName, String password) throws InterruptedException {
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div/div[3]/a")).click();
        sleep(5000);
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#nickname")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#policy")).click();
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.findElement(By.cssSelector("body > div > div > div.text-center.text-muted.mt-3 > a")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        sleep(2000);
    }

    /**
     * 成功登录
     * @param userName
     * @param password
     * @param url
     * @throws InterruptedException
     */
    @Order(2)
    @ParameterizedTest
    @MethodSource("dataRight")
    public void LoginSuccess(String userName, String password, String url) throws InterruptedException {
        System.out.println("userName:" + userName + "\npassword:" + password + "\nurl:" + url);
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(5000);
        webDriver.navigate().refresh();
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        Assertions.assertEquals(url, webDriver.getCurrentUrl());
        System.out.println("登录成功：" + userName);
    }

    /**
     * 发布帖子
     * @throws InterruptedException
     */

    @Order(3)
    @ParameterizedTest
    @MethodSource("titlePost")
    public void postText(String title) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/a[1]")).click();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        String text = webDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")).getText();
//        Assertions.assertEquals("发新帖 ", text);
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        WebElement element = webDriver.findElement(By.cssSelector("#article_post_borad"));
        webDriver.findElement(By.cssSelector("#article_post_borad")).click();
        Select select = new Select(element);
        select.selectByIndex(3);
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        String text_post = webDriver.findElement(By.cssSelector("#article_post_borad")).getText();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#article_post_title")).sendKeys(title);
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        sleep(2000);
        //找到输入行
        webDriver.findElement(By.xpath("//*[@id=\"edit-article\"]/div[1]/div[6]/div[1]/div/div/div")).click();
        sleep(5000);
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        WebElement span = webDriver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        Thread.sleep(3000);
        Actions action = new Actions(webDriver);
        action.doubleClick(span).sendKeys(Keys.DELETE).perform();
        action.sendKeys("这是第一篇博客内容").perform();
        //((JavascriptExecutor) webDriver).executeScript("arguments[0].textContent='这是第一篇博客内容';", span);
        sleep(2000);
        //滑动到最底端，否则会找不到元素
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        sleep(2000);
        webDriver.findElement(By.xpath("//*[@id=\"article_post_submit\"]")).click();
        sleep(3000);
        //System.out.println(text_posts);
        System.out.println("发布成功:" + title);
    }

    /**
     * 查看个人中心-我的帖子
     */
    @ParameterizedTest
    @Order(4)
    @MethodSource("titlePost")
    public void PCenter(String title) {
        webDriver.navigate().refresh();
        webDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[3]")).click();
        webDriver.findElement(By.cssSelector("#index_user_profile")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        String text = webDriver.findElement(By.cssSelector("#profile_articleCount")).getText();
        //帖子数量不为0
        Assertions.assertNotEquals("0", text);
        String title_cur = webDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li[1]/div[2]/div/div/div/div/div[1]/a/strong")).getText();
        //发布的是否是之前的帖子
        Assertions.assertEquals(title, title_cur);
        System.out.println("查看成功：" + title);
    }


    /**
     * 删除已发布的帖子
     * @param url
     * @throws InterruptedException
     */

    @ParameterizedTest
    @Order(5)
    @MethodSource("url")
    public void deletePost(String url) throws InterruptedException {
//        webDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li[1]/div[2]/div/div/div/div/div[1]/a")).click();
//        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//        sleep(5000);
        sleep(2000);
        webDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li[1]/div[2]/div/div/div/div/div[1]/a")).click();
        webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[1]/div[2]/div[2]/div/div[3]/div/a")).click();
        sleep(1000);
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#details_artile_delete")).click();
        sleep(3000);
        String url_cur = webDriver.getCurrentUrl();
        Assertions.assertEquals(url, url_cur);
        System.out.println("url"+url);
        //因为会有“成功发布帖子的链接出现”，所以需要刷新一下，否则一直处于无法找到元素的状态
        webDriver.navigate().refresh();
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        String text = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
        Assertions.assertEquals("java2", text);
        System.out.println("删除成功，首页第一篇帖子标题" + text);
    }

    /**
     * 退出登录
     * @param url
     * @throws InterruptedException
     */
    @ParameterizedTest
    @MethodSource("urlSignUp")
    @Order(6)
    public void quitCase(String url) throws InterruptedException {
        webDriver.navigate().refresh();
        sleep(3000);
        String url_cur = webDriver.getCurrentUrl();
        System.out.println("url_cur_before:" + url_cur);
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#index_user_logout")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        url_cur = webDriver.getCurrentUrl();
        System.out.println("url_cur_after:" + url_cur);
        Assertions.assertEquals(url, url_cur);
        System.out.println("退出成功");
    }

    /**
     * 错误的用户名或者密码会登陆失败，并检查弹出链接是否是“用户名或密码错误”
     * @param userName
     * @param password
     * @param url
     */

    @Disabled
    @Order(10)
    @ParameterizedTest
    @MethodSource("dataFailed")
    public void LoginFailed(String userName, String password, String url) throws InterruptedException {
        System.out.println("userName:" + userName + "\npassword:" + password );
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#submit")).click();
        String url_cur = webDriver.getCurrentUrl();
        System.out.println("url_cur:" + url_cur);
        //测试登陆失败后是否会跳转至别的页面
        Assertions.assertEquals(url, url_cur);
        //是否会弹出警告并且警告内容是否一致
        //服务器崩溃会提醒“内部问题，请联系管理员操作”
        String text = webDriver.findElement(By.xpath("/html/body/div[2]/div[1]")).getText();
        sleep(5000);
        //这里需要的是最后一行的信息
        String[] alert_text = text.split("\n");
        System.out.println(alert_text[alert_text.length-1]);
        Assertions.assertEquals("用户名或密码错误", alert_text[alert_text.length-1]);
    }

}
